home *** CD-ROM | disk | FTP | other *** search
/ University of Pittsburgh…tware Toolkit - Fall 2002 / Toolkit.iso / mac / Macintosh 9.x / Connectivity / Kerberos for Mac 4.0 / Mac OS 9 SDK 4.0 / Kerberos / ClassicGlue / ErrorLib.glue.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-20  |  15.9 KB  |  446 lines  |  [TEXT/CWIE]

  1. /* $Copyright:
  2.  *
  3.  * Copyright 1998-2000 by the Massachusetts Institute of Technology.
  4.  * 
  5.  * All rights reserved.
  6.  * 
  7.  * Export of this software from the United States of America may require a
  8.  * specific license from the United States Government.  It is the
  9.  * responsibility of any person or organization contemplating export to
  10.  * obtain such a license before exporting.
  11.  * 
  12.  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and distribute
  13.  * this software and its documentation for any purpose and without fee is
  14.  * hereby granted, provided that the above copyright notice appear in all
  15.  * copies and that both that copyright notice and this permission notice
  16.  * appear in supporting documentation, and that the name of M.I.T. not be
  17.  * used in advertising or publicity pertaining to distribution of the
  18.  * software without specific, written prior permission.  Furthermore if you
  19.  * modify this software you must label your software as modified software
  20.  * and not distribute it in such a fashion that it might be confused with
  21.  * the original MIT software. M.I.T. makes no representations about the
  22.  * suitability of this software for any purpose.  It is provided "as is"
  23.  * without express or implied warranty.
  24.  * 
  25.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  26.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  27.  * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  28.  * 
  29.  * Individual source code files are copyright MIT, Cygnus Support,
  30.  * OpenVision, Oracle, Sun Soft, FundsXpress, and others.
  31.  * 
  32.  * Project Athena, Athena, Athena MUSE, Discuss, Hesiod, Kerberos, Moira,
  33.  * and Zephyr are trademarks of the Massachusetts Institute of Technology
  34.  * (MIT).  No commercial use of these trademarks may be made without prior
  35.  * written permission of MIT.
  36.  * 
  37.  * "Commercial use" means use of a name in a product or other for-profit
  38.  * manner.  It does NOT prevent a commercial firm from referring to the MIT
  39.  * trademarks in order to convey information (although in doing so,
  40.  * recognition of their trademark status should be given).
  41.  * $
  42.  */
  43.  
  44. /* $Header: /cvs/macdev/mit/lib/ErrorLib/ClassicGlue/ErrorLib.glue.c,v 1.3 2001/03/20 02:33:16 meeroh Exp $ */
  45.  
  46. #include <CodeFragments.h>
  47. #include <Gestalt.h>
  48. #include <Errors.h>
  49.  
  50. #include <KerberosSupport/ErrorLib.h>
  51. #include "ErrorLib.glue.h"
  52.  
  53. /* These functions must obey CFM calling conventions. Functions which return
  54.    pointers must return them in D0, not A0 like ThinkC static 68k does.  This way
  55.    we can call CFM functions by pointer from here (if they are called by pointer
  56.    then the compiler can't tell ahead of time to do D0->A0 translation because it
  57.    doesn't know what calling convention the functions use). 
  58.    
  59.    Note that if it is necessary (if you don't use MPWC calling conventions) 
  60.    the D0->A0 translation will be done by the compiler in the places where 
  61.    the application calls these glue routines. */
  62. #pragma d0_pointers on
  63.  
  64. /* Hardcode library fragment name here */
  65. #define kLibraryName "\pMIT Support•ErrorLib"
  66.  
  67. /* Private function prototypes */
  68.  
  69. static OSErr Find_Symbol(
  70.     Ptr* pSymAddr,
  71.     Str255 pSymName,
  72.     ProcInfoType pProcInfo);
  73.  
  74. static pascal Boolean HaveCFM(void);
  75.  
  76. static pascal OSErr GetSystemArchitecture(OSType *archType);
  77.  
  78.  
  79. /* This code is directly from Technote 1077 */
  80. /* changed Library name to be hardcoded at the top of the file
  81.    instead in the middle of the code */
  82.  
  83. /* Private functions */
  84.  
  85. static pascal OSErr GetSystemArchitecture(OSType *archType)
  86. {
  87.     static long sSysArchitecture = 0; // static so we only Gestalt once.
  88.     OSErr tOSErr = noErr;
  89.  
  90.     *archType = kAnyCFragArch;   // assume wild architecture
  91.  
  92.     // If we don't know the system architecture yet...
  93.     if (sSysArchitecture == 0)
  94.     // ...Ask Gestalt what kind of machine we are running on.
  95.     tOSErr = Gestalt(gestaltSysArchitecture, &sSysArchitecture);
  96.  
  97.     if (tOSErr == noErr) // if no errors
  98.     {
  99.         if (sSysArchitecture == gestalt68k)   // 68k?
  100.             *archType = kMotorola68KCFragArch;   
  101.         else if (sSysArchitecture == gestaltPowerPC) // PPC?
  102.             *archType = kPowerPCCFragArch;       
  103.         else
  104.             tOSErr = gestaltUnknownErr;  // who knows what might be next?
  105.     }
  106.     return tOSErr;
  107. }
  108.  
  109. static pascal Boolean HaveCFM(void)
  110. {
  111.     long response;
  112.     return ( (Gestalt (gestaltCFMAttr, &response) == noErr) &&
  113.                 (((response >> gestaltCFMPresent) & 1) != 0));
  114. }
  115.  
  116. static OSErr Find_Symbol(
  117.     Ptr* pSymAddr,
  118.     Str255 pSymName,
  119.     ProcInfoType pProcInfo)
  120. {
  121.     static CFragConnectionID sCID = 0;
  122.     static OSType sArchType = kAnyCFragArch;
  123.     static OSErr sOSErr = noErr;
  124.  
  125.     Str255 errMessage;
  126.     Ptr mainAddr;
  127.     CFragSymbolClass symClass;
  128.     ISAType tISAType;
  129.  
  130.     if (sArchType == kAnyCFragArch)  // if architecture is undefined...
  131.     {
  132.         sCID = 0;     // ...force (re)connect to library
  133.         sOSErr = GetSystemArchitecture(&sArchType); // determine architecture
  134.         if (sOSErr != noErr)
  135.         return sOSErr; // OOPS!
  136.     }
  137.     
  138.     if (!HaveCFM()) {
  139.         // If we don't have CFM68K, return a reasonable-looking error.
  140.         sOSErr = cfragLibConnErr;
  141.         return sOSErr;
  142.     }
  143.  
  144.     if (sArchType == kMotorola68KCFragArch) // ...for CFM68K
  145.         tISAType = kM68kISA | kCFM68kRTA;
  146.     else if (sArchType == kPowerPCCFragArch)  // ...for PPC CFM
  147.         tISAType = kPowerPCISA | kPowerPCRTA;
  148.     else
  149.         sOSErr = gestaltUnknownErr; // who knows what might be next?
  150.  
  151.     if (sCID == 0) // If we haven't connected to the library yet...
  152.     {
  153.         // NOTE: The library name is hard coded here.
  154.         // I try to isolate the glue code, one file per library.
  155.         // I have had developers pass in the Library name to allow
  156.         // plug-in type support. Additional code has to be added to
  157.         // each entry points glue routine to support multiple or
  158.         // switching connection IDs.
  159.         sOSErr = GetSharedLibrary(kLibraryName, sArchType, kLoadCFrag,
  160.         &sCID, &mainAddr, errMessage);
  161.         if (sOSErr != noErr)
  162.         return sOSErr; // OOPS!
  163.     }
  164.  
  165.     // If we haven't looked up this symbol yet...
  166.     if ((Ptr) *pSymAddr == (Ptr) kUnresolvedCFragSymbolAddress)    
  167.     {
  168.         // ...look it up now
  169.         sOSErr = FindSymbol(sCID,pSymName,pSymAddr,&symClass);
  170.         if (sOSErr != noErr) // in case of error...
  171.         // ...clear the procedure pointer
  172.         *(Ptr*) &pSymAddr = (Ptr) kUnresolvedCFragSymbolAddress;
  173. #    if !GENERATINGCFM // if this is classic 68k code...
  174.             *pSymAddr = (Ptr)NewRoutineDescriptorTrap((ProcPtr) *pSymAddr,
  175.             pProcInfo, tISAType);  // ...create a routine descriptor...
  176. #    endif
  177.     }
  178.     return sOSErr;
  179. }
  180.  
  181.  
  182. /* CFM Glue Code for exported functions! */
  183.  
  184. /**** GetMITLibError ****/
  185. /* OSStatus GetMITLibError(void); */
  186.  
  187. enum {
  188.   GetMITLibError_ProcInfo = kThinkCStackBased 
  189.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  190.  
  191. };
  192.  
  193.  
  194. typedef OSStatus (*GetMITLibError_ProcPtrType)(void);
  195. OSStatus GetMITLibError (void)
  196. {
  197.   static GetMITLibError_ProcPtrType GetMITLibError_ProcPtr = kUnresolvedCFragSymbolAddress;
  198.     
  199.   // if this symbol has not been setup yet...
  200.   if((Ptr) GetMITLibError_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  201.     Find_Symbol((Ptr *) &GetMITLibError_ProcPtr, "\pGetMITLibError", GetMITLibError_ProcInfo);
  202.   if((Ptr) GetMITLibError_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  203.     return  GetMITLibError_ProcPtr();
  204. }
  205.  
  206.  
  207. /**** SetMITLibError ****/
  208. /* void SetMITLibError(OSStatus theError); */
  209.  
  210. enum {
  211.   SetMITLibError_ProcInfo = kThinkCStackBased 
  212.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  213. };
  214.  
  215.  
  216. typedef void (*SetMITLibError_ProcPtrType)(OSStatus);
  217. void SetMITLibError (OSStatus theError)
  218. {
  219.   static SetMITLibError_ProcPtrType SetMITLibError_ProcPtr = kUnresolvedCFragSymbolAddress;
  220.     
  221.   // if this symbol has not been setup yet...
  222.   if((Ptr) SetMITLibError_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  223.     Find_Symbol((Ptr *) &SetMITLibError_ProcPtr, "\pSetMITLibError", SetMITLibError_ProcInfo);
  224.   if((Ptr) SetMITLibError_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  225.       SetMITLibError_ProcPtr(theError);
  226. }
  227.  
  228.  
  229. /**** ClearMITLibError ****/
  230. /* void ClearMITLibError(void); */
  231.  
  232. enum {
  233.   ClearMITLibError_ProcInfo = kThinkCStackBased 
  234.  
  235. };
  236.  
  237.  
  238. typedef void (*ClearMITLibError_ProcPtrType)(void);
  239. void ClearMITLibError (void)
  240. {
  241.   static ClearMITLibError_ProcPtrType ClearMITLibError_ProcPtr = kUnresolvedCFragSymbolAddress;
  242.     
  243.   // if this symbol has not been setup yet...
  244.   if((Ptr) ClearMITLibError_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  245.     Find_Symbol((Ptr *) &ClearMITLibError_ProcPtr, "\pClearMITLibError", ClearMITLibError_ProcInfo);
  246.   if((Ptr) ClearMITLibError_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  247.       ClearMITLibError_ProcPtr();
  248. }
  249.  
  250.  
  251. /**** RegisterErrorTable ****/
  252. /* OSStatus RegisterErrorTable(const FSSpec* inResFile, SInt16 inResID); */
  253.  
  254. enum {
  255.   RegisterErrorTable_ProcInfo = kThinkCStackBased 
  256.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  257.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(const FSSpec *)))
  258.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(SInt16)))
  259. };
  260.  
  261.  
  262. typedef OSStatus (*RegisterErrorTable_ProcPtrType)(const FSSpec *, SInt16);
  263. OSStatus RegisterErrorTable (const FSSpec * inResFile,
  264.                              SInt16 inResID)
  265. {
  266.   static RegisterErrorTable_ProcPtrType RegisterErrorTable_ProcPtr = kUnresolvedCFragSymbolAddress;
  267.     
  268.   // if this symbol has not been setup yet...
  269.   if((Ptr) RegisterErrorTable_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  270.     Find_Symbol((Ptr *) &RegisterErrorTable_ProcPtr, "\pRegisterErrorTable", RegisterErrorTable_ProcInfo);
  271.   if((Ptr) RegisterErrorTable_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  272.     return  RegisterErrorTable_ProcPtr(inResFile, inResID);
  273. }
  274.  
  275.  
  276. /**** GetErrorLongFormat ****/
  277. /* OSStatus GetErrorLongFormat(OSStatus error, char *message, long messageLength); */
  278.  
  279. enum {
  280.   GetErrorLongFormat_ProcInfo = kThinkCStackBased 
  281.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  282.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  283.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  284.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  285. };
  286.  
  287.  
  288. typedef OSStatus (*GetErrorLongFormat_ProcPtrType)(OSStatus, char *, long);
  289. OSStatus GetErrorLongFormat (OSStatus error,
  290.                              char * message,
  291.                              long messageLength)
  292. {
  293.   static GetErrorLongFormat_ProcPtrType GetErrorLongFormat_ProcPtr = kUnresolvedCFragSymbolAddress;
  294.     
  295.   // if this symbol has not been setup yet...
  296.   if((Ptr) GetErrorLongFormat_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  297.     Find_Symbol((Ptr *) &GetErrorLongFormat_ProcPtr, "\pGetErrorLongFormat", GetErrorLongFormat_ProcInfo);
  298.   if((Ptr) GetErrorLongFormat_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  299.     return  GetErrorLongFormat_ProcPtr(error, message, messageLength);
  300. }
  301.  
  302.  
  303. /**** GetErrorShortFormat ****/
  304. /* OSStatus GetErrorShortFormat(OSStatus error, char *message, long messageLength); */
  305.  
  306. enum {
  307.   GetErrorShortFormat_ProcInfo = kThinkCStackBased 
  308.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  309.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  310.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  311.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  312. };
  313.  
  314.  
  315. typedef OSStatus (*GetErrorShortFormat_ProcPtrType)(OSStatus, char *, long);
  316. OSStatus GetErrorShortFormat (OSStatus error,
  317.                               char * message,
  318.                               long messageLength)
  319. {
  320.   static GetErrorShortFormat_ProcPtrType GetErrorShortFormat_ProcPtr = kUnresolvedCFragSymbolAddress;
  321.     
  322.   // if this symbol has not been setup yet...
  323.   if((Ptr) GetErrorShortFormat_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  324.     Find_Symbol((Ptr *) &GetErrorShortFormat_ProcPtr, "\pGetErrorShortFormat", GetErrorShortFormat_ProcInfo);
  325.   if((Ptr) GetErrorShortFormat_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  326.     return  GetErrorShortFormat_ProcPtr(error, message, messageLength);
  327. }
  328.  
  329.  
  330. /**** GetErrorManager ****/
  331. /* OSStatus GetErrorManager(OSStatus error, char *message, long messageLength); */
  332.  
  333. enum {
  334.   GetErrorManager_ProcInfo = kThinkCStackBased 
  335.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  336.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  337.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  338.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  339. };
  340.  
  341.  
  342. typedef OSStatus (*GetErrorManager_ProcPtrType)(OSStatus, char *, long);
  343. OSStatus GetErrorManager (OSStatus error,
  344.                           char * message,
  345.                           long messageLength)
  346. {
  347.   static GetErrorManager_ProcPtrType GetErrorManager_ProcPtr = kUnresolvedCFragSymbolAddress;
  348.     
  349.   // if this symbol has not been setup yet...
  350.   if((Ptr) GetErrorManager_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  351.     Find_Symbol((Ptr *) &GetErrorManager_ProcPtr, "\pGetErrorManager", GetErrorManager_ProcInfo);
  352.   if((Ptr) GetErrorManager_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  353.     return  GetErrorManager_ProcPtr(error, message, messageLength);
  354. }
  355.  
  356.  
  357. /**** GetErrorShortString ****/
  358. /* OSStatus GetErrorShortString(OSStatus error, char *message, long messageLength); */
  359.  
  360. enum {
  361.   GetErrorShortString_ProcInfo = kThinkCStackBased 
  362.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  363.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  364.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  365.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  366. };
  367.  
  368.  
  369. typedef OSStatus (*GetErrorShortString_ProcPtrType)(OSStatus, char *, long);
  370. OSStatus GetErrorShortString (OSStatus error,
  371.                               char * message,
  372.                               long messageLength)
  373. {
  374.   static GetErrorShortString_ProcPtrType GetErrorShortString_ProcPtr = kUnresolvedCFragSymbolAddress;
  375.     
  376.   // if this symbol has not been setup yet...
  377.   if((Ptr) GetErrorShortString_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  378.     Find_Symbol((Ptr *) &GetErrorShortString_ProcPtr, "\pGetErrorShortString", GetErrorShortString_ProcInfo);
  379.   if((Ptr) GetErrorShortString_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  380.     return  GetErrorShortString_ProcPtr(error, message, messageLength);
  381. }
  382.  
  383.  
  384. /**** GetErrorLongString ****/
  385. /* OSStatus GetErrorLongString(OSStatus error, char *message, long messageLength); */
  386.  
  387. enum {
  388.   GetErrorLongString_ProcInfo = kThinkCStackBased 
  389.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  390.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  391.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  392.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  393. };
  394.  
  395.  
  396. typedef OSStatus (*GetErrorLongString_ProcPtrType)(OSStatus, char *, long);
  397. OSStatus GetErrorLongString (OSStatus error,
  398.                              char * message,
  399.                              long messageLength)
  400. {
  401.   static GetErrorLongString_ProcPtrType GetErrorLongString_ProcPtr = kUnresolvedCFragSymbolAddress;
  402.     
  403.   // if this symbol has not been setup yet...
  404.   if((Ptr) GetErrorLongString_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  405.     Find_Symbol((Ptr *) &GetErrorLongString_ProcPtr, "\pGetErrorLongString", GetErrorLongString_ProcInfo);
  406.   if((Ptr) GetErrorLongString_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  407.     return  GetErrorLongString_ProcPtr(error, message, messageLength);
  408. }
  409.  
  410.  
  411. /**** GetErrorString ****/
  412. /* OSStatus GetErrorString(OSStatus error, char *message, long messageLength, ErrorFormat format); */
  413.  
  414. enum {
  415.   GetErrorString_ProcInfo = kThinkCStackBased 
  416.   | RESULT_SIZE(SIZE_CODE(sizeof(OSStatus)))
  417.   | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(OSStatus)))
  418.   | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *)))
  419.   | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
  420.   | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(ErrorFormat)))
  421. };
  422.  
  423.  
  424. typedef OSStatus (*GetErrorString_ProcPtrType)(OSStatus, char *, long, ErrorFormat);
  425. OSStatus GetErrorString (OSStatus error,
  426.                          char * message,
  427.                          long messageLength,
  428.                          ErrorFormat format)
  429. {
  430.   static GetErrorString_ProcPtrType GetErrorString_ProcPtr = kUnresolvedCFragSymbolAddress;
  431.     
  432.   // if this symbol has not been setup yet...
  433.   if((Ptr) GetErrorString_ProcPtr == (Ptr) kUnresolvedCFragSymbolAddress)
  434.     Find_Symbol((Ptr *) &GetErrorString_ProcPtr, "\pGetErrorString", GetErrorString_ProcInfo);
  435.   if((Ptr) GetErrorString_ProcPtr != (Ptr) kUnresolvedCFragSymbolAddress)
  436.     return  GetErrorString_ProcPtr(error, message, messageLength, format);
  437. }
  438.  
  439.  
  440. Boolean ErrorLibraryIsPresent(void)
  441. {
  442.     Ptr    symAddr;
  443.     return (Find_Symbol (&symAddr, "\pGetMITLibError", GetMITLibError_ProcInfo)) == noErr;
  444. }
  445.  
  446.